﻿using Microsoft.Extensions.Logging;
using Service.Core.Repository.Core;
using Service.Framework.ApplicationEventBus;
using Service.Framework.SmsRecordLog;

namespace Service.Core.Repository.Event
{
    public class SmsRecordLogHandle : IApplicationEventHandler<SmsRecordLogAges>
    {
        private readonly ILogger<SmsRecordLogHandle> logger;
        private readonly IRepository<SmsRecordLog> repository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public SmsRecordLogHandle(ILogger<SmsRecordLogHandle> logger, IRepository<SmsRecordLog> repository)
        {
            this.logger = logger;
            this.repository = repository;
        }

        public async Task Handle(SmsRecordLogAges @event)
        {
            logger.LogInformation("SavaSmsRecordLog");
            await repository.Db.Insertable(ConvertToSmsRecordLog(@event.SmsRecordLogDto)).SplitTable().ExecuteCommandAsync();
        }


        public SmsRecordLog ConvertToSmsRecordLog(SmsRecordLogDto dto)
        {
            return new SmsRecordLog(dto.SupplierName, dto.TemplateName, dto.TemplateParam, dto.SignName, dto.PhoneNumber, dto.Success, dto.Msg);
        }
    }
}
